package com.test38_集合;

import org.junit.Test;

import java.util.Stack;

/**
 * Stack栈，先进后出(FILO)
 */
public class Demo06_Stack {

    @Test
    public void test1() {
        Stack<String> stack = new Stack<>();
        System.out.println("【是否为空栈】" + stack.empty());
        stack.push("沐言优拓：www.yootk.com");
        stack.push("VIP课程大纲：edu.yootk.com");
        stack.push("李兴华编程训练营：yooytk.ke.qq.com");
        while (!stack.empty()) {
            System.out.println("【栈顶弹出数据】" + stack.pop());
        }
    }

    /**
     * 字符串反转
     */
    @Test
    public void test2() {
        String str = "www.yootk.com";
        char[] data = str.toCharArray();
        Stack<Character> stack = new Stack<>();
        for (int x = 0; x < data.length; x++) {
            stack.push(data[x]);    // 入栈
        }
        while (!stack.empty()) {
            System.out.print(stack.pop());  // 出栈
        }
    }

    /**
     * 空栈异常
     */
    @Test
    public void test3() {
        Stack<String> stack = new Stack<>();
        stack.push("沐言优拓：www.yootk.com");
        System.out.println("【第一次弹出】" + stack.pop());
        System.out.println("【第二次弹出】" + stack.pop());
    }
}
